<?php
// +----------------------------------------------------------------------
// | 场次管理
// +----------------------------------------------------------------------
// | Author: TongBC <501468940@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/03/06
// +----------------------------------------------------------------------
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/func.inc.php';
include_once '/var/www/html/new/d/class/Api.class.php';
include_once '/var/www/html/new/d/module/pft_show/PftShow.class.php';

include_once '/var/www/html/new/d/module/common/Db.class.php';
$dbConf = include '/var/www/html/new/d/module/common/db.conf.php';// 远端服务器配置信息

use \PFT\Db;
Db::Conf($dbConf['remote_1']);
if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"登陆超时","code":0}');
$obj = new \PFT\Show\PftShow(Db::Connect());
switch(trim($_REQUEST['action'])) {
    case 'SAVE_ROUND':
//        print_r($_POST);
        $venus_id = intval($_POST['venus_id']);
        $rounds   = $_POST['round'];
        $dates    = explode(',', $_POST['dates']);
        if (!count($dates)) {
            Api::Response('适用日期不能为空',Api::badRequestCode);
        }

        foreach ($dates as $date){
            foreach ($rounds as $rid=>$round) {
                $ret = $obj->CreateRound(0, $venus_id, $round['round_name'], $round['bt'],
                    $round['et'], $date, $round['pre_sale'], $_SESSION['sid'], 0, $rid);
                if ($ret['status']!='ok') {
                    Api::Response($ret['msg'], Api::badRequestCode);
                    break;
                }
            }
        }
        Api::Response('保存成功', Api::okCode);
        break;

    case 'SYNC_ROUND':
//        print_r($_POST);
//        exit;
        $venus_id = intval($_POST['venus_id']);
        $sync_date = $_POST['sync_date'];
        $use_date_list    = explode(',', $_POST['dates']);
        $ret = $obj->SyncRound($venus_id, $sync_date , $use_date_list);
        if ($ret['status']!='ok') {
            Api::Response($ret['msg'], Api::badRequestCode);
        }
        Api::Response('同步成功', Api::okCode);
        break;
    case 'SET_ROUND_STATE':
        $rid = intval($_POST['rid']);
        $state = intval($_POST['status']);
        if ( !$rid || $rid<0 ) Api::Response('场馆编号出错', Api::badRequestCode);
        if ($state !=0 && $state !=1 ) Api::Response('状态出错', Api::badRequestCode);
        if ($obj->SetRoundState($rid, $state)) {
            Api::Response('设置成功', Api::okCode);
        }
        Api::Response('设置失败', Api::badRequestCode);
        break;
    //获取场次座位状态 成功：code=200
    case 'ROUND_SEAT_GET':
        $round_id = intval($_GET['round_id']);
        $arr = $obj->DynSeatGet($round_id);
        if ($arr['status']=='ok') {
            $out = array('code'=>200, 'collection'=>$arr['list'], 'zones'=>$arr['zones']);
            echo json_encode($out);
            exit;
//            Api::CollectionResponse($arr['list']);
        }
        Api::Response($arr['msg'], Api::badRequestCode);
        break;
    case 'ROUND_SEAT_SET':
//        print_r(file_get_contents('php://input'));
//        print_r($_GET);
//        exit;
        $round_id = intval($_REQUEST['round_id']);
        if (!$round_id) Api::Response('场次ID不能为空',Api::badRequestCode);
//        $arr = file_get_contents('php://input');
        $arr = $_POST['zones'];
//        print_r($arr);exit;
//        print_r($_POST);exit;
//        if (!count($arr)) Api::Response('请选择座位', Api::badRequestCode);
        $err = array();
        foreach($arr as $zone_id=>$seat_list) {
            foreach ($seat_list as $seat_id=>$seat_state) {
                try {
//                    echo "$round_id, 0,$seat_id, $seat_state, $zone_id \n" ;
//                    continue;
//                    if ($seat_state!=4) continue;
                    if ($seat_state == 0) continue;//场次里面的座位没有关闭，只有不可售
                    $ret = $obj->DynSeatSet($round_id, 0,$seat_id, $seat_state, $zone_id);
                } catch (ErrorException $e) {
                    $err[] = array($seat_id, $e->getMessage());
                }
            }
        }
//        if (count($err)) {
//            Api::Response('有部分座位设置失败', Api::badRequestCode);
//        }
        Api::Response('设置成功', Api::okCode);
        break;
    case 'ROUND_SEAT_SET_OLD':
//        print_r(file_get_contents('php://input'));
//        print_r($_GET);
//        exit;
        $round_id = intval($_REQUEST['round_id']);
        if (!$round_id) Api::Response('场次ID不能为空',Api::badRequestCode);
//        $arr = file_get_contents('php://input');
        $seat_list = $_POST;
//        print_r($arr);exit;
//        print_r($_POST);exit;
//        if (!count($arr)) Api::Response('请选择座位', Api::badRequestCode);
        $err = array();
        foreach ($seat_list as $seat_id=>$seat_state) {
            try {
                if ($seat_state == 0) continue;//场次里面的座位没有关闭，只有不可售
                $ret = $obj->DynSeatSet($round_id, 0,$seat_id, $seat_state, $zone_id);
            } catch (ErrorException $e) {
                $err[] = array($seat_id, $e->getMessage());
            }
        }


//        if (count($err)) {
//            Api::Response('有部分座位设置失败', Api::badRequestCode);
//        }
        Api::Response('设置成功', Api::okCode);
        break;
    default:break;
}

// 数据转义
function _addslashes($data){
    if(is_array($data)){
        foreach($data as $key=>$value){
            $data[$key] = _addslashes($value);
        }
        return $data;
    }
    return addslashes($data);
}

// 生成语句
function _insertSqlBuild($data, $table){
    if(is_null($data)) return '';
    $sql = 'insert into `'.$table.'` set ';
    foreach($data as $field=>$value){
        $sql = $sql.'`'.$field.'`="'.$value.'",';
    }
    return rtrim($sql,',');
}
// 生成编辑语句
function _updateSqlBuild($data, $table){
    if(is_null($data)) return '';
    $sql = 'update `'.$table.'` set ';
    foreach($data as $field=>$value){
        $sql = $sql.'`'.$field.'`="'.$value.'",';
    }
    $sql = rtrim($sql,',');
    return $sql;
}
?>